<?php
class IPInformation {
	public static function getHtmlContent($urlSite) {
		$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
		// create curl resource
        $ch = curl_init();

        // set user agent
		curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
		
        // set url
        curl_setopt($ch, CURLOPT_URL, $urlSite);

        //return the transfer as a string
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        
        // $output contains the output string
        $output = curl_exec($ch);

        // Print the curl info like http response code, content type etc.
//		echo '<pre>';
//		print_r (curl_getinfo($ch));
//		echo '</pre>';
        
        // close curl resource to free up system resources
        curl_close($ch);

        return $output;
	}
	
	public static function getIPInformation() {
		if (!empty($_SERVER['HTTP_CLIENT_IP'])) {   //check ip from share internet
			$ip = $_SERVER['HTTP_CLIENT_IP'];
		} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {   //to check ip is pass from proxy
			$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
		} else {
			$ip = $_SERVER['REMOTE_ADDR'];
		}
		if ($ip == '::1') {
			$ip = '58.186.204.36';	
		}
		$ip = '118.68.49.38';
//		$ip = '210.245.85.55';
		return $ip;
	}
	
	public static function getRegionInformation($urlSite, &$lat, &$long) {
		$htmlContent = IPInformation::getHtmlContent($urlSite);
		if (isset($htmlContent) && $htmlContent != '') {
			$htmlDoc = new DOMDocument();
			$htmlDoc->loadHTML($htmlContent);
			$xpath = new DOMXPath($htmlDoc); 
			$latitude = $xpath->query("//div[@id='section_content']//tr[th='Latitude:']/td");
			$longitude = $xpath->query("//div[@id='section_content']//tr[th='Longitude:']/td");
			if ($latitude->length > 0) {
				$lat = $latitude->item(0)->textContent;
				$long = $longitude->item(0)->textContent;
			}
		} 
	}	
}
?>